package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.ReportDao;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 报表服务实现类
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao orderDao;


    /**
     * 查询套餐预约占比和数量
     * @return
     */
    @Override
    public Map getSetmealReport() {
        // 获取数据
        List<Map<String,Object>> resultList = reportDao.getSetmealReport();
        // 遍历数据加入list获取每一个name的值,作为html中需要的legend中data需要的[]数据
        ArrayList<Object> list = new ArrayList<>();
        if (resultList != null && resultList.size() > 0) {
            for (Map<String, Object> map : resultList) {
                // 获取套餐名
                list.add(map.get("name"));
            }
        }

        HashMap<String, Object> resultMap = new HashMap<>();
        // 需要的数据存入resultMap中
        resultMap.put("setmealNames",list);
        resultMap.put("setmealCount",resultList);

        return resultMap;

    }

    /**
     * 查询用户报表数据
     * @return
     */
    @Override
    public Map getMemberReport() {
        // 获取当前时间
        Calendar calendar = Calendar.getInstance();
        // 当前月份回退12个月
        calendar.add(Calendar.MONTH,-12);
        // 放置月份信息
        ArrayList<String> months = new ArrayList<>();
        // 放置会员计数信息
        ArrayList<Integer> memberCount = new ArrayList<>();
        // 遍历获取每个月yyy-MM格式
        for (int i = 0; i < 12; i++) {
            calendar.add(Calendar.MONTH,1);
            Date date = calendar.getTime();
            // date to string
            String month = new SimpleDateFormat("yyyy-MM").format(date);
            months.add(month);
            // 获取截止该月前会员的数量
            int count = memberDao.getMemberCountByMonth(month + "-31");
            // 存入数据
            memberCount.add(count);

        }

        HashMap<String, Object> resultMap = new HashMap<>();
        // 放入数据返回
        resultMap.put("months",months);
        resultMap.put("memberCount",memberCount);
        // 返回结果
        return resultMap;

    }


    /**
     * 运营数据获取
     * @return
     */
    @Override
    public Map getBusinessReportData() throws Exception {

        // *********日期获取**********

        // 获取当前日期
        Date today = DateUtils.getToday();
        String reportDate = DateUtils.parseDate2String(today);
        // 获取本周第一天(中国人的周一)
        Date thisWeekMonday = DateUtils.getThisWeekMonday();
        String monday = DateUtils.parseDate2String(thisWeekMonday);
        // 获取本周的最后一天
        Date sundayOfThisWeek = DateUtils.getSundayOfThisWeek();
        String sunday = DateUtils.parseDate2String(sundayOfThisWeek);
        // 获取本月第一天
        Date firstDay4ThisMonth = DateUtils.getFirstDay4ThisMonth();
        String firstDay = DateUtils.parseDate2String(firstDay4ThisMonth);
        // 获取本月最后一天
        Date lastDay4ThisMonth = DateUtils.getLastDay4ThisMonth();
        String lastDay = DateUtils.parseDate2String(lastDay4ThisMonth);
        // 日期信息map
        HashMap<String, String> weekMap = new HashMap<>();
        HashMap<String, String> monthMap = new HashMap<>();
        weekMap.put("startDate",monday);
        weekMap.put("endDate",sunday);
        monthMap.put("startDate",firstDay);
        monthMap.put("endDate",lastDay);

        // *********获取会员相关信息*************

        // 查询当日新增会员数量
        int todayNewMember = memberDao.getMemberCountOfToday(reportDate);
        // 查询总会员数量
        int totalMember = memberDao.getMemberTotalCount();
        // 查询本周新增会员数量
        int thisWeekNewMember = memberDao.getMemberCountBetweenDates(weekMap);
        // 查询本月新增会员数量
        int thisMonthNewMember = memberDao.getMemberCountBetweenDates(monthMap);


        // *********获取预约相关信息*************


        // 查询当日新增预约数量
        int todayOrderNumber = orderDao.getOrderCountOfToday(reportDate);
        // 查询本周新增预约数量
        int thisWeekOrderNumber = orderDao.getOrderCountBetweenDates(weekMap);
        // 查询本月新增预约数量
        int thisMonthOrderNumber = orderDao.getOrderCountBetweenDates(monthMap);

        // 查询当日到诊人数
        int todayVisitsNumber = orderDao.getVisitsCountOfToday(reportDate);
        // 查询本周到诊人数
        int thisWeekVisitsNumber = orderDao.getVisitsCountBetweenDates(weekMap);
        // 查询本月到诊人数
        int thisMonthVisitsNumber = orderDao.getVisitsCountBetweenDates(monthMap);

        // 查询热点套餐信息(前四)
        List<Map> hotSetmeal = reportDao.getHotSetmeal();

        // 数据存入
        HashMap<String, Object> resultMap = new HashMap<>();
        resultMap.put("reportDate",reportDate);
        resultMap.put("totalMember",totalMember);
        resultMap.put("todayNewMember",todayNewMember);
        resultMap.put("thisWeekNewMember",thisWeekNewMember);
        resultMap.put("thisMonthNewMember",thisMonthNewMember);
        resultMap.put("todayOrderNumber",todayOrderNumber);
        resultMap.put("thisWeekOrderNumber",thisWeekOrderNumber);
        resultMap.put("thisMonthOrderNumber",thisMonthOrderNumber);
        resultMap.put("todayVisitsNumber",todayVisitsNumber);
        resultMap.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        resultMap.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        resultMap.put("hotSetmeal",hotSetmeal);

        // 返回结果
        return resultMap;

    }

}
